package com.littleken.qrcode;

import com.swetake.util.Qrcode;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class CreateQRCode_qrcode {
    public static void main(String[] args) throws IOException {
        //实例化二维码生成对象
        Qrcode x = new Qrcode();
        x.setQrcodeErrorCorrect('M');//设置纠错等级
        x.setQrcodeEncodeMode('B');//N代表数字，A代表a-z，B代码其他字符
        int version = 7;
        x.setQrcodeVersion(version);//二维码版本号
        String qrData = "com/littleken";//内容
        int width = 67 + 12 * (version - 1);
        int height = 67 + 12 * (version - 1);
        BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D gs = bufferedImage.createGraphics();
        gs.setBackground(Color.WHITE);
        gs.setColor(Color.BLACK);
        gs.clearRect(0,0,width,height);


        int pixoff = 2;//偏移量

        byte[] d = qrData.getBytes("gb2312");
        if(d.length > 0 && d.length < 120){
            boolean[] [] s = x.calQrcode(d);

            for (int i = 0; i <s.length ; i++) {
                for (int j = 0; j < s.length; j++) {
                    if (s[j][i]){
                        gs.fillRect(j*3 + pixoff,i * 3 + pixoff,3,3);
                    }
                }
            }
        }
        gs.dispose();
        bufferedImage.flush();
        ImageIO.write(bufferedImage,"png",new File("D://img2.png"));
    }
}
